<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */

get_header(); ?>

		<div id="primary">
			<div id="content" role="main"><div>
				
				<?php while ( have_posts() ) : the_post(); 
					// `get primary meta content
					$bad_count = 0;
					$bad_str = '';
                    $post_id = get_the_ID();
                    $preview_image = get_post_meta($post_id, 'wpcf-tip-preview-image', true);
                    $description = get_post_meta($post_id, 'wpcf-tip-description', true); 
					
                    $the_title = get_the_title(); 
                    if ($the_title == '') {
                    	$bad_str .=  '- Item title not found<br/>';
                    	$bad_count++;
                    }
                    else if (strlen($the_title) <= 10) {
                    	$bad_str .=  '- Item title too short (<=10)<br/>';
                    	$bad_count++;
                    }
                    else if (strpos($the_title, ' ') === false) {
                    	$bad_str .=  '- Item title needs more words<br/>';
                    	$bad_count++;
                    }
                                        
                    
                    if (!is_image($preview_image)) {
                    	$bad_count++;
                    	$bad_str .= '- Wrong item preview image src<br/>';
                    } else if (strpos($preview_image, esc_url(home_url()) . '/wp-content/uploads/') === false) {
						$bad_count++;
						$bad_str .= '- Item preview image must under '. esc_url(home_url()).'<br/>';						
					}
					
					
					if ($description == '') {
						$bad_str .=  '- Item description not found<br/>';
						$bad_count++;
					} else if (strlen($description) <= 150) {
						$bad_str .=  '- Item description too short (<=150)<br/>';
						$bad_count++;
					} else if (strpos($description, ' ') === false) {
						$bad_str .=  '- Item description need more words<br/>';
						$bad_count++;
					}
					
					?>
				
					<?php if ($bad_count == 0) :?>
						<h1><?php echo $the_title;?></h1>
		                <div class="post-body item-body" data="<?php echo $post_id; ?>">
		                	<div class="item-preview">
			                	<img class="preview-image" src="<?php echo $preview_image; ?>" alt="preview-image"/>
		                	</div>
		                	<div class="item-description">
		                		<?php 
		                		$description = apply_filters('the_content', $description);
		                		echo $description;
		                		?>
		                	</div>
		                </div>
		                
		                
						<?php comments_template( '', true ); ?>
					<?php else: ?>
						<h1>Opps, a bad item</h1>
						<p>Hi! We so sorry that we can not show you this item because it is a bad item by below reasons:</p>
						<?php echo $bad_str; ?>
						<p>Total bad: <strong><?php echo $bad_count;?></strong></p>
						<?php edit_post_link();?>
					<?php endif;?>

				<?php endwhile; // end of the loop. ?>

			</div></div><!-- #content -->			
			<?php get_sidebar(); ?>
		</div><!-- #primary -->
		<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/single.js"></script>
<?php get_footer(); ?>